﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    public class CWidgetData: ISaveData<CWidget, CWidget>
    {
        public bool isAttachedToHUD;
        public string parent;
        public string typeFullName;
        public string assemblyFullName;
        public Vector2 position = Vector2.Zero;
        public Vector2 size = Vector2.One;
        public CHudTextureData texture;
        public List<CWidgetData> children;
        public Vector2 minSize;
        public Vector2 maxSize;
        public bool isVisible = false;
        public string name;
        public SPositionsTBLR margins;


        public bool isInputConsumer;
        public bool isActive = true;

        public Rectangle rect;
        public Rectangle sourceRect;
        public Rectangle drawableRect;
        public bool useDrawableRect = false;


        public Color color;

        public bool isContainer = false;
        public bool isContainerGroup = false;

        public bool isAlwaysOntop = false;

        public bool isClickable = true;

        public static CWidgetData CreateWidgetDataType(CWidget wid)
        {
            AttrTypeDependentSerializable att = wid.GetType().GetCustomAttributes(typeof(AttrTypeDependentSerializable), false).First() as AttrTypeDependentSerializable;
            Type t = att.TypeOfDataStructure;
            if (t == null)
                t = typeof(CWidgetData);
            CWidgetData cdata = (CWidgetData)t.GetConstructor(new Type[0]).Invoke(new object[0]);
            return cdata;
        }

        public virtual void FillDataStructure(CWidget w)
        {
            if (w.HUD.DisplayList.Contains(w))
                isAttachedToHUD = true;
            if (w.Parent != null)
                parent = w.Parent.Name;
            CWidget wid = w;
            typeFullName = w.GetType().FullName;
            assemblyFullName = w.GetType().Assembly.FullName;
            name = w.Name;
            color = w.Color;
            drawableRect = w.DrawableRect;
            isClickable = w.IsClickable;
            isAlwaysOntop = w.IsAlwaysOnTop;
            isContainer = w.IsContainer;
            isContainerGroup = w.IsContainerGroup;
            isInputConsumer = w.IsInputConsumer;
            isVisible = w.IsVisible;
            margins = w.Margins;
            //maxSize = w.MaxSize;
            //minSize = w.MinSize;
            position = w.Position;
            rect = w.Rect;
            size = w.Size;
            if (w.Texture != null)
            {
                texture = new CHudTextureData();
                texture.FillDataStructure(w.Texture);
            }
            useDrawableRect = w.UseDrawableRect;

            children = new List<CWidgetData>();
            foreach (CWidget child in w.Children.Values)
            {
                CWidgetData wd = CWidgetData.CreateWidgetDataType(child);
                wd.FillDataStructure(child);
                children.Add(wd);
            }
        }

        public virtual void FillFromDataStructure(CWidget wid)
        {

        }

        /// <summary>
        /// Fills the object with its saved values.
        /// </summary>
        /// <param name="go"></param>
        public virtual void FillFromDataStructure(ref CWidget wid, CHUDSystem hud)
        {
//          parent;
            if (parent != null)
            {
                wid.Parent = hud.GetElement(parent);
            }
            else
                hud.Add(wid);
            //if (wid.HUD == null)
            //    wid.HUD = hud;
            wid.Position = position;
            wid.Size = size;
            wid.Color = color;
            wid.DrawableRect = drawableRect;
            wid.IsActive = isActive;
            wid.IsAlwaysOnTop = isAlwaysOntop;
            wid.IsClickable = isClickable;
            wid.IsContainer = isContainer;
            wid.IsContainerGroup = isContainerGroup;
//            wid.IsFixedSize = isFixedSize;
            wid.IsInputConsumer = isInputConsumer;
            wid.Margins = margins;
            //wid.MaxSize = maxSize;
            //wid.MinSize = minSize;
//            name;
//            rect;
//            sourceRect;
            if (texture != null)
            {
                CHudTexture tex = new CHudTexture();
                texture.FillFromDataStructure(tex);
                wid.Texture = tex.Clone();
                if (texture.m_anim != null)
                {
                    wid.Texture.AnimationState = texture.m_anim.Clone();
                    
                }
            }
            wid.UseDrawableRect = useDrawableRect;


            if (isVisible)
                wid.Show();
        }
    }
}
